﻿// Copyright © Microsoft Corporation.  All Rights Reserved.
// This code released under the terms of the 
// Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)

using Microsoft.Phone.Controls;
using TheOliver.Phone.BMI.Strings;
using Microsoft.Phone.Shell;
using System;
using System.Threading;

namespace TheOliver.Phone.BMI
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();

            var cc = Thread.CurrentThread.CurrentCulture;

            this.Loaded += (s, e) =>
            {
                SetApplicationBarButtons();
            };
        }

        ApplicationBarIconButton _historyButton;
        ApplicationBarIconButton _saveButton;

        private void SetApplicationBarButtons()
        {
            _saveButton = (ApplicationBarIconButton)ApplicationBar.Buttons[0];
            _saveButton.Text = Strings.Strings.Save;
            _saveButton.Click += (s, e) =>
                {
                    SaveCurrentBMI();
                };

            _historyButton = (ApplicationBarIconButton)ApplicationBar.Buttons[1];
            _historyButton.Text = Strings.Strings.History;
            _historyButton.Click += (s, e) =>
                {
                    NavigationService.Navigate(new System.Uri("/History.xaml", System.UriKind.Relative));
                };
        }

        private void SaveCurrentBMI()
        {
            Settings settings = (Settings)App.Current.Resources["SettingsDataSource"];
            BMIDataContext db = new BMIDataContext();
            db.AddBmi(settings.BMI);

            

            //string dBConnectionString = "Data Source=isostore:/Database.sdf";

            //using (BMIDataContext db = new BMIDataContext(dBConnectionString))
            //{
            //    if (!db.DatabaseExists())
            //    {
            //        db.CreateDatabase();
            //    }

            //    BMIItem newBmi = new BMIItem();
            //    newBmi.Bmi = settings.BMI;
            //    newBmi.Date = DateTime.Now;
            //    db.Items.InsertOnSubmit(newBmi);

            //    db.SubmitChanges();
            //}
        }

        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedFrom(e);
            UpdateLiveTile();
        }

        private void UpdateLiveTile()
        {
            Settings settings = (Settings)App.Current.Resources["SettingsDataSource"];

            StandardTileData NewTileData = new StandardTileData
            {
                BackTitle = Strings.Strings.CurrentBMI,
                BackContent = settings.BMI.ToString("#.#")
            };

            foreach (var b in ShellTile.ActiveTiles)
            {
                b.Update(NewTileData);
            }
        }
    }
}